#!/usr/bin/env python3
#coding=utf8

from threading import Thread

class QueueThread(object):
    """队列模式的多线程，会一直等待队列中增加元素。
    任务完成后不会自动退出，直到手动调用STOP退出"""
    def __init__(self,Queue,func):
        self.Queue      = Queue
        self.func       = func
        self.threads    = set()
        self.FLAG       = True
        self.STAT       = False

    def run(self):
        while self.FLAG:
            if self.STAT:
                time.sleep(0.1)
                continue
            data = self.Queue.get()
            self.func(data)

    def start(self,num_thread=10):
        for i in range(num_thread):
            self.threads[i] = Thread(target = self.run)
        loops = range(len(self.threads))
        for j in loops:
            #self.threads[j].setDaemon(True)
            self.threads[j].start()
        for k in loops:
            self.threads[k].join()

    def stop(self):
        self.FLAG = False

    def pause(self):
        self.STAT = not self.STAT

    def clear(self):
        self.Queue.clear()

    def isdone(self):
        return True and self.Queue.qsize()

class IterThread(object):
    """列表模式的多线程，列表中的任务完成后会自动退出，
    也可手动调用stop退出，这样有可能列表中的任务会没有完成"""
    def __init__(self,Queue,func):
        self.Queue      = iter(Queue)
        self.func       = func
        self.threads    = set()
        self.FLAG       = True
        self.STAT       = False

    def run(self,data):
        self.func(data)

    def start(self,num_thread=10):
        while self.FLAG:
            if self.STAT:
                time.sleep(0.1)
                continue
            for i in range(num_thread):
                try:
                    data = next(self.Queue)
                except StopIteration:
                    self.FLAG = False
                    break
                self.threads[i] = Thread(target = self.run, args=(data,))
            loops = range(len(self.threads))
            for j in loops:
                #self.threads[j].setDaemon(True)
                self.threads[j].start()
            for k in loops:
                self.threads[k].join()

    def stop(self):
        self.FLAG = False

    def pause(self):
        self.STAT = not self.STAT

class DBHelper(object):
    def __init__(self, dbname):
        pass
        #self.db = getattr(models,dbname)

    def __call__(self,func):
        def handler(*args):
            result = func(*args)
            if result:
                pass
                #print(func,*args)
                #self.db.get_or_create(**data)
        return handler

class BaseUrl(object):
    def __init__(self,url):
        self.url        = url           #当前url
        self.head       = None          #协议
        self.domain     = None          #域
        self.pagepath   = None          #当前文件路径
        self.pagename   = None          #当前文件名称
        self.args       = {}            #参数
        if '://' in self.url:
            self.head,urlpath = url.split('://')
            if '/' in urlpath:
                domainpath = urlpath.split('/')
                self.domain = domainpath[0]
                if '?' in urlpath:
                    self.path = urlpath.split('?')[0]
                    self.args = '?'.join(urlpath.split('?')[1:])
                else:
                    self.path = '/'.join(domainpath[1:])
            else:
                self.domain = urlpath
    
    def getkwargs(self):
        return dict([arg.split("=") for arg in self.args.split("&")])